package com.tokenplus.thai.extend

import android.content.ContextWrapper
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.tokenplus.thai.ProjectConfig
import com.tokenplus.thai.utils.analytics.util.AnalyticsUtil

fun View.dip2Px(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue * scale + 0.5F).toInt()
}

fun View.px2Dip(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue / scale + 0.5F).toInt()
}

fun View.getParentActivity(): FragmentActivity? {
    var gcontext = context
    while (gcontext is ContextWrapper) {
        if (gcontext is AppCompatActivity) {
            return (gcontext as FragmentActivity)
        }
        gcontext = gcontext.baseContext
    }
    return null
}

fun View.getScenClickFlag(page: String): String {
    return "${ProjectConfig.product}_${page}_${getSafeTag()}_CLICK"
}

fun View.getScenPageStartFlag(page: String): String {
    return "${ProjectConfig.product}_${page}_START"
}

fun View.getScenPageEndFlag(page: String): String {
    return "${ProjectConfig.product}_${page}_END"
}

fun View.getSafeTag(): String {
    if (tag != null) {
        if (tag is String) {
            return tag as String
        }
    }
    return ""
}

fun View.recordEvent(
    type: String,
    remark: String? = System.currentTimeMillis().toString()
) {
    if (type.isNotBlank()) {
        try {
            var gRemark = remark
            if (gRemark.isNullOrBlank()) {
                gRemark = System.currentTimeMillis().toString()
            }
            AnalyticsUtil.saveHitPointInfo(
                type = type,
                remark = gRemark
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun View.recordTagEvent(
    remark: String? = System.currentTimeMillis().toString()
) {
    recordEvent(getSafeTag(), remark)
}

fun View.recordClickEvent(
    remark: String? = System.currentTimeMillis().toString()
) {
    try {
        val gTag = getSafeTag()
        if (getSafeTag().isNotBlank()) {
            recordEvent("${ProjectConfig.product}_${gTag}_CLICK", remark)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordSelectEvent(
    remark: String? = System.currentTimeMillis().toString()
) {
    try {
        val gTag = getSafeTag()
        if (getSafeTag().isNotBlank()) {
            recordEvent("${ProjectConfig.product}_${gTag}_SELECT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordInputEvent(
    remark: String? = System.currentTimeMillis().toString()
) {
    try {
        val gTag = getSafeTag()
        if (getSafeTag().isNotBlank()) {
            recordEvent("${ProjectConfig.product}_${gTag}_INTPUT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.toBeFocusPoint() {
    try {
        if (this is EditText) {
            setSelection(text.toString().trim().length)
        }
        isFocusable = true
        isFocusableInTouchMode = true
        requestFocus()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}